package com.hdu.point_offer;

public class lcr171 {

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    class Solution {
        ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            int lenA = 0;
            int lenB = 0;
            ListNode curA = headA;
            while (curA != null) {
                lenA++;
                curA = curA.next;
            }
            ListNode curB = headB;
            while (curB != null) {
                lenB++;
                curB = curB.next;
            }
            int diff = Math.abs(lenA - lenB);
            curA = headA;
            curB = headB;
            if (lenA > lenB)
            {
                while (diff-- > 0) {
                    curA = curA.next;
                }
            }
            if (lenB > lenA)
            {
                while (diff-- > 0) {
                    curB = curB.next;
                }
            }
            while (curA != null) {
                if (curA == curB) {
                    return curA;
                }
                curA = curA.next;
                curB = curB.next;
            }
            return null;
        }
    }
}
